package com.sgroup.studycountry.api.restful;

import com.sgroup.studycountry.application.dto.LoginInput;
import com.sgroup.studycountry.application.dto.UserInput;
import com.sgroup.studycountry.application.service.IUserService;
import com.sgroup.studycountry.support.rest.R;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @author umi
 * @date 2021/10/27 19:31
 */
@AllArgsConstructor
@RestController("/user")
@ResponseBody
public class UserController {

    private final IUserService userService;

    @GetMapping("/{userId}")
    public R<?> getById(@PathVariable("userId") String userId) {
        return R.data(userService.getById(userId));
    }

    @PostMapping
    public R<?> createUser(@RequestBody UserInput userInput) {
        return R.data(userService.insert(userInput));
    }

    @PutMapping("/{userId}")
    public R<?> createUser(@PathVariable("userId") String userId,
                           @RequestBody UserInput userInput) {
        return R.data(userService.update(userId, userInput));
    }

    @PostMapping("/login")
    public R<?> login(@RequestBody LoginInput loginInput){
        userService.login(loginInput.getPhoneNumber(),loginInput.getPassword());
        return R.ok();
    }

}
